package tg.edu.fhh.demo.service;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import tg.edu.fhh.demo.entity.TbFile;
import tg.edu.fhh.demo.repository.TbfileRepository;
import tg.edu.fhh.demo.web.form.TbFileForm;


@Service
public class TbFileService {

	@Autowired
	private TbfileRepository fileDAO;
	
	@Value("${web.file-path}")
	private String path;
	

  public <S extends TbFile> S save(S entity) {
		return fileDAO.save(entity);
	}
	public <S extends TbFile> S findOne(Example<S> example) {
		Optional<S> optional = fileDAO.findOne(example);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	public TbFile findOne(Specification<TbFile> spec) {
		Optional<TbFile> optional = fileDAO.findOne(spec);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;

	}
	public Page<TbFile> findAll(Pageable pageable) {
		return fileDAO.findAll(pageable);
	}
	public List<TbFile> findAll() {
		return fileDAO.findAll();
	}
	public List<TbFile> findAll(Sort sort) {
		return fileDAO.findAll(sort);
	}
	public List<TbFile> findAll(Specification<TbFile> spec) {
		return fileDAO.findAll(spec);
	}
	public List<TbFile> findAllById(Iterable<Integer> ids) {
		return fileDAO.findAllById(ids);
	}
	public Page<TbFile> findAll(Specification<TbFile> spec, Pageable pageable) {
		return fileDAO.findAll(spec, pageable);
	}
	public <S extends TbFile> List<S> saveAll(Iterable<S> entities) {
		return fileDAO.saveAll(entities);
	}
	public TbFile findById(Integer id) {
		Optional<TbFile> optional = fileDAO.findById(id);
		if (optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	public void flush() {
		fileDAO.flush();
	}
	public List<TbFile> findAll(Specification<TbFile> spec, Sort sort) {
		return fileDAO.findAll(spec, sort);
	}
	public <S extends TbFile> S saveAndFlush(S entity) {
		return fileDAO.saveAndFlush(entity);
	}
	public boolean existsById(Integer id) {
		return fileDAO.existsById(id);
	}
	public void deleteInBatch(Iterable<TbFile> entities) {
		fileDAO.deleteInBatch(entities);
	}
	public long count(Specification<TbFile> spec) {
		return fileDAO.count(spec);
	}
	public <S extends TbFile> Page<S> findAll(Example<S> example, Pageable pageable) {
		return fileDAO.findAll(example, pageable);
	}
	public void deleteAllInBatch() {
		fileDAO.deleteAllInBatch();
	}
	public TbFile getOne(Integer id) {
		return fileDAO.getOne(id);
	}
	public <S extends TbFile> long count(Example<S> example) {
		return fileDAO.count(example);
	}
	public <S extends TbFile> boolean exists(Example<S> example) {
		return fileDAO.exists(example);
	}
	public <S extends TbFile> List<S> findAll(Example<S> example) {
		return fileDAO.findAll(example);
	}
	public long count() {
		return fileDAO.count();
	}
	public void deleteById(Integer id) {
		fileDAO.deleteById(id);
	}
	public <S extends TbFile> List<S> findAll(Example<S> example, Sort sort) {
		return fileDAO.findAll(example, sort);
	}
	public void delete(TbFile entity) {
		fileDAO.delete(entity);
	}
	public void deleteAll(Iterable<? extends TbFile> entities) {
		fileDAO.deleteAll(entities);
	}
	public void deleteAll() {
		fileDAO.deleteAll();
	}
@Transactional
	public void upload(TbFileForm form) {
		TbFile model=new TbFile();
		Integer id = form.getId();
		if(id!=null) {
			model=findById(id);
		}
		BeanUtils.copyProperties(form, model,"id");
		MultipartFile multipartFile = form.getFile();
		String originalFilename = multipartFile.getOriginalFilename();
		if(StringUtils.hasText(originalFilename)) {
			String uuid = model.getUuid();
			if(uuid!=null) {
				File f=new File(path,uuid);
				f.delete();
			}
			model.setFilename(originalFilename);
			uuid = UUID.randomUUID().toString();
			model.setUuid(uuid);
			File f=new File(path, uuid);
			try {
				multipartFile.transferTo(f);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		save(model);
		
	}
  @Transactional
	public boolean delete(Integer id) {
		try {
			TbFile model = findById(id);
			String uuid = model.getUuid();
			File file=new File(path,uuid);
			if(file.exists()) {
				file.delete();
			}
			delete(model);
			return true;
		}catch (Exception e) {
			return false;
		}
	}
	

	
}
